
$("#get-button").click(function () {
   var url = $("#get-url").val();
    getGoods(url);
});

function getGoods(req_url){
    console.log(req_url);
    currentIndex = 1;
    $.ajax({
        type: 'post',
        url: '/admin/gather/post',
        dataType: "json",
        async: false,
        data:{'url':req_url,_token: LA.token,},
        cache: true,
        success: function(json){
            currentIndex++;
            var jsondata = json.data;
            if(jsondata != ''){
                var str = ' <p class="font-line">正在从<span class="color-red">' +
                    jsondata.next_url+'</span>获取</p>\n' +
                    '<p class="font2-line"> <span>一共' +
                    jsondata.total+'条商品数据，' +
                    jsondata.page_number+'页数据，正在获取第</span> <span class="color-red">' +
                    jsondata.curpage+'</span> 页</p>';
                setTimeout(getGoods, 2000,jsondata.next_url);

            }else {
                var str = "数据获取成功";
            }
            $("#modal-body").html(str);
            $("#good-Modal").modal('show');
        },
        error:function () {
            alert("请求失败");
        }
    });
}



/**
 * [ajaxHttp ajax请求方法]
 * @Author   王文凡
 * @DateTime 2017-10-13
 * @version  1.0
 * @param    {[type]}   param [description]
 * @return   {[type]}         [description]
 */
function ajaxHttp(param) {
    var settings = $.extend({

        type: 'get',
        async: true,
        cache: 'false',
        dataType: 'json'
    }, param);
    if (settings.btn) {
        if (settings.btn.hasClass('disabled')) {
            return false;
        }
        settings.btn.addClass('disabled');
    }
    $.ajax({
        type: settings.type,
        cache: settings.cache,
        async: settings.async,
        dataType: settings.dataType,
        data: settings.data,
        url: settings.url,
        success: function(data) {
            if (data.status == true) {
                if ($.isFunction(settings.success)) {
                    settings.success(data);
                } else {
                    $.pjax.reload('#pjax-container');
                    toastr.success(data.message);
                }

            } else {
                if ($.isFunction(settings.fail)) {
                    settings.fail(data);
                } else {
                    toastr.error(data.message);
                }
            }
        },
        complete: function(XMLHttpRequest, textStatus) {

            var ret_code = XMLHttpRequest.status;
            var ret_json = XMLHttpRequest.responseJSON;
            var message = {};
            for (var i in ret_json) {
                message[i] = ret_json[i][0];
            }
            if (ret_code == 422) {
                for (var p in message) {
                    toastr.error(message[p]);
                    break;
                }
            } else if (textStatus == 'error') {
                toastr.error(ret_json.message ? ret_json.message : '请求失败');
            }
        },
        error: function(data) {
            //toastr.error(data.Msg);
        }
    });
}


// const aesjs = require('aes-js');
// const request = require('superagent');
const encrypt = () => {
    const timestamp = Date.now();
    const userid = '212257903313158199';
    const securityKey = Math.floor(1e8 * Math.random());
    const i = `${timestamp}:${userid}:${securityKey}`;
    let list = [];
    const e = encodeURI(i);
    for (let i = 0; i < e.length; i++) {
        list.push(e.charCodeAt(i));
    }
    list = new Uint8Array(list);
    const a = [68, 107, 79, 108, 105, 87, 118, 70, 78, 82, 55, 67, 52, 87, 118, 82];
    const b = new Uint8Array([71, 81, 87, 75, 85, 69, 50, 67, 86, 71, 79, 79, 66, 75, 88, 85]);
    const key = new Uint8Array(a);
    const aesCfb = new aesjs.ModeOfOperation.cfb(key, b);
    const encryptedBytes = aesCfb.encrypt(list);
    const token = aesjs.utils.hex.fromBytes(encryptedBytes);
    return {
        timestamp,
        userid,
        securityKey,
        token
    };
};
const getHeader = () => {
    const {
        timestamp,
        userid,
        securityKey,
        token
    } = encrypt();
    return {
        'x-api-version': '0.0.3',
        'x-user-id': userid,
        'x-request-token': token,
        'x-security-key': securityKey,
        'x-timestamp': timestamp,
        'x-request-misc': {
            "platform": "browser"
        }
    };
};

// console.log(getHeader());

function dzySync(req_url='https://www.duozhuayu.com/api/user/cart') {
    currentIndex = 1;
    $.ajax({
        type: 'post',
        url: '/admin/cart/post',
        dataType: "json",
        async: false,
        data:{'req_url':req_url,'headers':getHeader(),_token: LA.token,},
        cache: true,
        success: function(json){
            currentIndex++;
            var jsondata = json.data;
            if(jsondata != ''){
                var str = ' <p class="font-line">正在从<span class="color-red">' +
                    jsondata.next_url+'</span>获取</p>\n' +
                    '<p class="font2-line"> <span>一共' +
                    jsondata.total+'条商品数';
                    // +正在获取第</span> <span class="color-red">' +
                    // jsondata.curpage+'</span> 页</p>';
                setTimeout(dzySync, 2000,jsondata.next_url);

            }else {
                var str = "数据获取成功";
            }
            $("#modal-body").html(str);
            $("#good-Modal").modal('show');
        },
        error:function () {
            alert("请求失败");
        }
    });
}



